<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改订单详情')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-orderDetail-edit" th:object="${orderDetail}">
            <input name="orderDetailId" th:field="*{orderDetailId}" type="hidden">
            <div class="form-group hidden">
                <label class="col-sm-3 control-label">合同id：</label>
                <div class="col-sm-8">
                    <input name="contractId" th:field="*{contractId}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group hidden">
                <label class="col-sm-3 control-label">合同类型：</label>
                <div class="col-sm-8">
                    <input name="contractType" th:field="*{contractType}" class="form-control" type="text">
                </div>
            </div>

            <div class="form-group hidden">
                <label class="col-sm-3 control-label">订单id：</label>
                <div class="col-sm-8">
                    <input name="orderId" th:field="*{orderId}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group hidden" >
                <label class="col-sm-3 control-label">产品id：</label>
                <div class="col-sm-8">
                    <input name="productId" th:field="*{productId}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group hidden">
                <label class="col-sm-3 control-label">仓库Id：</label>
                <div class="col-sm-8">
                    <input name="warehouseId" th:field="*{warehouseId}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">价格：</label>
                <div class="col-sm-8">
                    <input name="price" th:field="*{price}" class="form-control" readonly type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label" id="numId">数量：</label>
                <div class="col-sm-8">
                    <input name="num" th:field="*{num}" class="form-control" readonly type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label" id="useNumId">入库数量：</label>
                <div class="col-sm-8">
                    <input name="useNum" th:field="*{useNum}" class="form-control" readonly type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label" id="remainNumId">剩余数量：</label>
                <div class="col-sm-8">
                    <input name="remainNum" th:field="*{remainNum}" class="form-control" readonly type="text">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label" id="operationNumId">入库数量：</label>
                <div class="col-sm-8">
                    <input name="operationNum" th:value="*{remainNum}" class="form-control"  type="number">
                </div>
            </div>
            <div class="form-group hidden">
                <label class="col-sm-3 control-label">供应商ID：</label>
                <div class="col-sm-8">
                    <input name="supplierId" th:field="*{supplierId}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <textarea name="remark" class="form-control">[[*{remark}]]</textarea>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
       // var prefix = ctx + "warehouse/orderDetail";

        $("#form-orderDetail-edit").validate({
            focusCleanup: true
        });

       let prefix=null;
        let contractType = [[${orderDetail.contractType}]];
        if(contractType==1){//销售合同
            prefix=ctx + "warehouse/sellStoreDetail";
            $("#useNumId").text("已出库数量");
            $("#remainNumId").text("待出库数量");
            $("#operationNumId").text("本次出库数量");
        }else if(contractType==0){//采购合同
            prefix=ctx + "warehouse/mallStoreDetail";
            $("#useNumId").text("已入库数量");
            $("#remainNumId").text("待入库数量");
            $("#operationNumId").text("本次入库数量");
        }
        function submitHandler() {
            let remainNum= $("input[name=remainNum]").val();
            let operationNum =$("input[name=operationNum]").val();
            if(contractType==0){//入库
               if(remainNum==0){
                   $.modal.alertError("已经完成入库");
                   return;

               }
               if(operationNum<1){
                   $.modal.alertError("入库数量必须>0");
                   return;
               }
                if(operationNum>remainNum){
                    $.modal.alertError("【入库数量："+operationNum+"】 不能大于 【待入库数量："+remainNum+"】");
                    return;
                }
            }

            if(contractType==1){
                if(operationNum<1){
                    $.modal.alertError("出库数量必须>0");
                    return;
                }
            }

            if ($.validate.form()) {
                let list=[];
                list.push($('#form-orderDetail-edit').serializeObject())
                var datas = {"params[list]":JSON.stringify(list)};
                $.operate.save(prefix + "/add", datas);
            }
        }
       $.fn.serializeObject = function() {
           var o = {};
           var a = this.serializeArray();
           $.each(a, function() {
               if (o[this.name]) {
                   if (!o[this.name].push) {
                       o[this.name] = [ o[this.name] ];
                   }
                   o[this.name].push(this.value || '');
               } else {
                   o[this.name] = this.value || '';
               }
           });
           return o;
       }
    </script>
</body>
</html>
